package com.xhwl.data.controller;


import com.xhwl.common.dto.cent.sdata.RoleTypeDTO;
import com.xhwl.common.enums.LogTypeEnum;
import com.xhwl.common.enums.config.RoleTypeEnum;
import com.xhwl.common.es.EsPage;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Enterprise;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.data.service.IEnterpriseService;
import com.xhwl.data.service.IProjectService;
import com.xhwl.data.service.IRoleService;
import com.xhwl.starter.log.LogQuery;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.data.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 系统日志
 *
 * @Author JCccc
 * @Description
 * @Date 2021/6/29 16:21
 */
@RestController
public class LogController {
    @Autowired
    ILogService logService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IProjectService projectService;
    @Autowired
    IEnterpriseService enterpriseService;


    /**
     * 查询日志
     *
     * @param logQuery
     * @return
     */
    @PostMapping(value = "/log/list")
    public ResultJson esquery(
            Account operateAccount,
            @RequestHeader(value = "appId", required = true) String appId,
            @RequestHeader(value = "clientType", required = false) Integer clientType,
            @RequestHeader(value = "industryId", required = true) Short industryId,
            @RequestBody LogQuery logQuery) {
        EsPage pageList = search(operateAccount, appId, clientType, industryId, logQuery);
        return ResultJson.success(pageList);
    }

    public EsPage search(Account operateAccount, String appId, Integer clientType, Short industryId, LogQuery logQuery) {
        // 项目和企业不在前台创建了 不属于某个业态
        // logQuery.setIndustryId(industryId);
        if (LogTypeEnum.OPERATE_LOG.getType() == logQuery.getLogType()) {
            logQuery.setIndex(LogTypeEnum.OPERATE_LOG.getIndex());
        } else if (LogTypeEnum.LOGIN_LOG.getType() == logQuery.getLogType()) {
            logQuery.setIndex(LogTypeEnum.LOGIN_LOG.getIndex());
        } else {
            throw new BusinessException("暂不支持其他日志类型查询");
        }
//        //获取当前登录操作账号的角色身份
//        RoleTypeDTO accountRoleType = roleService.getAccountRoleType(operateAccount.getId());
//        RoleTypeEnum operateRoleType = accountRoleType.getRoleTypeEnum();

//        //获取当前登录账号能操作的项目ids
//        if (CollectionUtils.isEmpty(logQuery.getProjectIds())){
//            //超管不需要限制项目数据
//            if (!(RoleTypeEnum.SUPPER_MANAGER.getCode() == operateRoleType.getCode())) {
//                ProjectQuery projectQuery=new ProjectQuery();
//                List<Project> projects = projectService.list(projectQuery, operateAccount);
//                List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList());
//                logQuery.setProjectIds(projectIds);
//            }
//        }
//        //获取当前账号所在企业
//        List<Enterprise> enterprises = enterpriseService.list(null,operateAccount);
//        if (!CollectionUtils.isEmpty(enterprises)){
//            logQuery.setEntIds(enterprises.stream().map(Enterprise::getId).collect(Collectors.toList()));
//        }
        EsPage pageList = logService.getPageList(logQuery);
        return pageList;
    }

    /**
     * 日志导出
     *
     * @param logQuery
     * @return
     */
    @PostMapping(value = "/log/export")
    public void exportLog (
            HttpServletResponse response,
            Account operateAccount,
            @RequestHeader(value = "appId", required = true) String appId,
            @RequestHeader(value = "clientType", required = false) Integer clientType,
            @RequestHeader(value = "industryId", required = true) Short industryId,
            LogQuery logQuery) throws IOException {

        EsPage pageList = search(operateAccount, appId, clientType, industryId, logQuery);
        logService.exportLog(response, logQuery, pageList);
    }

    /**
     * 登录日志分页
     *
     * @param appId
     * @param industryId
     * @param query
     * @return
     */
    @PostMapping("/log/page-login-log")
    public ResultJson pageLoginLog(@RequestHeader(value = "appId") String appId,
                                   @RequestHeader(value = "industryId") Short industryId,
                                   @RequestHeader(value = "envId") Integer envId,
                                   @RequestBody LogQuery query) {
        EsPage esPage = logService.pageLoginLog(appId, industryId, envId, query);
        return ResultJson.success(esPage);
    }
}
